/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
            //基本思路是使用一个队列 把根节点先入队 然后根结点出队 然后入左右 计算数量 
            //然后左出队 入子树 右出队 入子树 然后再算数量 直到没了为止
            //
            vector<vector<int>> tree;
           
            queue<TreeNode*> nums;
            int layersize = 0;
            if(root)
            {
                nums.push(root);
                layersize=1;
            }
            while(!nums.empty())
            {

                 vector<int> n;
                 while(layersize--)
                 {
                    TreeNode* front = nums.front();
                    nums.pop();
                    if(front->left)
                    {
                        nums.push(front->left);
                    }
                    if(front->right)
                    {
                        nums.push(front->right);
                    }
                    n.push_back(front->val);
                 }
                tree.push_back(n);
                layersize = nums.size();
            }
            return tree;
    }
};
